
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
shady-css-parser
Advanced tools
The motivation for Shady CSS Parser is to provide a fast, small and flexible CSS parser suitable for facilitating runtime parsing and transformation of CSS. The Polymer library and the Polymer Designer tool are both example cases where fast and flexible CSS parsing and transformation is a critical feature.
With node
and npm
installed, run the following command:
npm install shady-css-parser
Run the following commands from the project root:
npm run build
This will create a dist
directory containing distributable artifacts.
import * as shadyCss from 'shady-css-parser';
const css = 'body { color: red; }';
const parser = new shadyCss.Parser();
const ast = parser.parse(css);
/* Step 1: Inherit from NodeFactory */
class CustomNodeFactory extends shadyCss.NodeFactory {
/*
* Step 2: Implement a custom node factory method. Here we override the
* default factory for Expression nodes
*/
expression(text) {
if (/^darken\(/.test(text)) {
return {
type: 'darkenExpression',
color: text.replace(/^darken\(/, '').replace(/\)$/, ''),
};
} else {
return super.expression.apply(this, arguments);
}
}
}
const css = 'body { color: darken(red); }';
/* Step 3: Instantiate a Parser with an instance of the specialized
* CustomNodeFactory */
const parser = new shadyCss.Parser(new CustomNodeFactory());
const ast = parser.parse(css);
const stringifier = new shadyCss.Stringifier();
stringifier.stringify(ast);
Note: the built-in Parser and Stringifier discard most insignficiant whitespace from parsed CSS.
/* Step 1: Inherit from Stringifier. */
class CustomStringifier extends shadyCss.Stringifier {
/**
* Step 2: Implement a stringification method named after the type of the node
* you are interested in stringifying. In this case, we are implementing
* stringification for the Darken Expression nodes we implemented parsing for
* above.
*/
darkenExpression(darkenExpression) {
// For the sake of brevity, please assume that the darken function returns
// a darker version of the color parameter:
return darken(darkenExpression.color);
}
}
/* Step 3: Use the custom stringifer: */
const stringifier = new CustomStringifier();
const css = stringifier.stringify(ast);
.container {
--nog: blue;
}
{
"type": 1, /* stylesheet */
"rules": [
{
"type": 4, /* ruleset */
"selector": ".container",
"rulelist": {
"type": 7, /* rulelist */
"rules": [
{
"type": 6, /* declaration */
"name": "--nog",
"value": {
"type": 5, /* expression */
"text": "blue"
}
}
]
}
}
]
}
ruleset {
--mixin-name: {
/* rules */
};
}
{
"type": 1, /* stylesheet */
"rules": [
{
"type": 4, /* ruleset */
"selector": "ruleset",
"rulelist": {
"type": 7, /* rulelist */
"rules": [
{
"type": 6, /* declaration */
"name": "--mixin-name",
"value": {
"type": 7, /* rulelist */
"rules": [
{
"type": 2, /* comment */
"value": "\/* rules *\/"
}
]
}
}
]
}
}
]
}
.title {
@apply(--my-toolbar-title-theme);
}
{
"type": 1, /* stylesheet */
"rules": [
{
"type": 4, /* ruleset */
"selector": ".title",
"rulelist": {
"type": 7, /* rulelist */
"rules": [
{
"type": 3, /* at rule */
"name": "apply",
"parameters": "(--my-toolbar-title-theme)",
"rulelist": null
}
]
}
}
]
}
/* unclosed
@fiz {
--huk: {
/* buz */
baz: lur;
};
}
{
"type": 1, /* stylesheet */
"rules": [
{
"type": 2, /* comment */
"value": "\/* unclosed\n@fiz {\n --huk: {\n \/* buz *\/"
},
{
"type": 6, /* declaration */
"name": "baz",
"value": {
"type": 5, /* expression */
"text": "lur"
}
},
{
"type": 8, /* discarded */
"text": "};\n"
},
{
"type": 8, /* discarded */
"text": "}"
}
]
}
/* before */
body {
margin: 0;
padding: 0px
}
/* after */
body{margin:0;padding:0px;}
/* before */
@import url('foo.css');
@font-face {
font-family: foo;
}
@charset 'foo';
/* after */
@import url('foo.css');@font-face{font-family:foo;}@charset 'foo';
/* before */
:root {
--qux: vim;
--foo: {
bar: baz;
};
}
#target {
gak: var(--qux);
@apply(--foo);
}
/* after */
:root{--qux:vim;--foo:{bar:baz;};}#target{gak:var(--qux);@apply (--foo);}
[0.1.0] - 2017-07-18
iterateOverAst
method, which returns an iterable over every
node in an AST.FAQs
A fast, small and flexible CSS parser.
We found that shady-css-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.